package com.eascs.admin.o2o.oaotodata.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.common.utils.StringUtils;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.oaotodata.service.OaO2oDataService;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.UrlUtil;
import com.eascs.continueloan.controller.O2oContinueRestController;
import com.eascs.file.FileTypeConstants;
import com.eascs.loansettled.entity.O2oLoanSettledFormEntity;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.tag.MenuTag;
import com.eascs.workflow.common.entity.sequence.SequenceEntity;
import com.eascs.workflow.common.service.sequence.SequenceService;

/**
* @author bao.pan 2016年12月25日 下午10:41:05
*/
@Controller
@RequestMapping("/oaoro2odata")
public class OaOrO2oDataController extends BaseMasterViewController {
	private static Logger logger = LoggerFactory.getLogger(OaOrO2oDataController.class);
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	@Reference
	private  SequenceService sequenceService;
	@RequestMapping(value="/listbuttonoa",method=RequestMethod.GET)
	public String listbutton() {
		return "oaoro2o/list_button";
	}
	@RequestMapping(value="/listoa",method=RequestMethod.GET)
	public String list() {
		return "oaoro2o/list_button";
	}
	@RequestMapping(value="/acclist",method=RequestMethod.GET)
	public String acclist() {
		return "oaoro2o/acc_list_button";
	}
	@RequestMapping("/index")
	public String findBankLoan(O2oLoanSettledFormEntity entity) {
		String o2ourl = "/oaoro2odata/index"; 
		if (entity != null) {
			String billCode = entity.getBillCode() == null ? "" : entity.getBillCode();
			String bankLoan = entity.getBankLoan() == null ? "" : entity.getBankLoan();
			String operName = entity.getOperName() == null ? "" : entity.getOperName();
			String loanUserName = entity.getLoanUserName() == null ? "" : entity.getLoanUserName();
			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
			String loanDateEnd = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			String createUserName = entity.getCreateUserName() == null ? "" : entity.getCreateUserName();
			String createDateStart = entity.getCreateDateStart() == null ? null : entity.getCreateDateStart();
			String createDateEnd = entity.getCreateDateEnd() == null ? null : entity.getCreateDateEnd();
			String dateDueStart = entity.getDateDueStart() == null ? null : entity.getDateDueStart();
			String dateDueEnd = entity.getDateDueEnd() == null ? null : entity.getDateDueEnd();
			String ltdName = entity.getLtdName() == null ? "" : entity.getLtdName();
			String ltd = entity.getLtd() == null ? "" : entity.getLtd();
			String loanType = entity.getLoanType() == null ? "" : entity.getLoanType();
			String province = entity.getProvince() == null ? "" : entity.getProvince();
			String gdStatus = entity.getGdStatus() == null ? "" : entity.getGdStatus();
			String repaystatus = entity.getRepaystatus() == null ? "" : entity.getRepaystatus();
			String idc = entity.getIdc() == null ? "" : entity.getIdc();
			String regionalName = entity.getRegionalName()== null ? "" : entity.getRegionalName();
			String brandName = entity.getBrandName()== null ? "" : entity.getBrandName();
			String partnerName = entity.getPartnerName()== null ? "" : entity.getPartnerName();
			String hostProvince = entity.getHostProvince()== null ? "" : entity.getHostProvince();
			o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
			o2ourl = UrlUtil.appendUrl(o2ourl, "bankLoan", bankLoan);
			o2ourl = UrlUtil.appendUrl(o2ourl, "operName", operName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanUserName", loanUserName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createUserName", createUserName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateStart", createDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateEnd", createDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "dateDueStart", dateDueStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "dateDueEnd", dateDueEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltdName", ltdName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltd", ltd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanType", loanType);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = UrlUtil.appendUrl(o2ourl, "gdStatus", gdStatus);
			o2ourl = UrlUtil.appendUrl(o2ourl, "repaystatus", repaystatus);
			o2ourl = UrlUtil.appendUrl(o2ourl, "idc", idc);
			o2ourl = UrlUtil.appendUrl(o2ourl, "regionalName", regionalName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "brandName", brandName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "partnerName", partnerName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "hostProvince", hostProvince);
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}
//		PageResult<Object[]> pageResult = loanSettledService.findByPage(entity);
//		String[] titles = { "贷款单号","借款人姓名","银行","平台名称","贷款类型","省区","放款金额","放款日期","到期日期","状态"};
//		SelectDataOption[] selectDataOptions = { null,  null,new SelectDataOption("BANKTYPEO2O"), null, new SelectDataOption("OTO_LOAN_TYPE"),null, null,  null,null,new SelectDataOption("SQZT") };
//		Button[] titleButtons = new Button[]{ButtonHelper.createAjaxButton(null, "/rloansettled/detail?loanapplyNo=")};
//		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles,o2ourl);
//		paginationInfo.setBuildPagingNavigation(true);
//		paginationInfo.setFirstRenderColumnIndex(1);
//		paginationInfo.setTitleButtons(titleButtons); 
//		paginationInfo.setBuildSelectBox(true);
//		paginationInfo.setSelectDataOptions(selectDataOptions);
//		paginationInfo.setFormats(new Format[] { null, null, null, null,null,null,new DecimalFormat(",##0.00"), null, null });
//		ViewContext.put("paginationInfo", paginationInfo);
//		// ViewContext.put("oaurl", oaurl);
//		ViewContext.put("vo", entity);
		return "oaoro2o/v1";
	}
	public  String buildCode(String code, String type) {
		String bill = StringUtils.getFourBitYearMonth(new Date());
		String prefix = code + "-"+ bill;
		String flag = sequenceService.getSeq(SequenceEntity.O2O_LOAN_NO, StatusContexts.O2O_SEQUENCE_LENGTH, prefix);
		if (flag == null) {
			throw new RuntimeException();
		}
		return flag;
	}
}
